package com.css.fxfzypg.modules.auxiliary.service.impl;

import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.auxiliary.entity.AuxiliaryMap;
import com.css.fxfzypg.modules.auxiliary.param.MapParam;
import com.css.fxfzypg.modules.auxiliary.param.QueryParam;
import com.css.fxfzypg.modules.auxiliary.repository.AuxiliaryMapRepository;
import com.css.fxfzypg.modules.auxiliary.service.AuxiliaryMapService;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.util.PlatformDateUtils;
import com.css.fxfzypg.util.PlatformSessionUtils;
import com.css.fxfzypg.util.UUIDGenerator;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class AuxiliaryMapServiceImpl implements AuxiliaryMapService {
    @Autowired
    private AuxiliaryMapRepository auxiliaryMapRepository;
    @Autowired
    private SUserService sUserService;
    @Override
    public void save(MapParam mapParam) {
        List<AuxiliaryMap> check = auxiliaryMapRepository.check(mapParam);
        if (check.size() != 0){
            List<String> ids = check.stream().map(AuxiliaryMap::getId).collect(Collectors.toList());
            auxiliaryMapRepository.deleteByIds(ids);
        }
        AuxiliaryMap auxiliaryMap = getAuxiliaryMap(mapParam);
        auxiliaryMapRepository.save(auxiliaryMap);
    }

    private AuxiliaryMap getAuxiliaryMap(MapParam mapParam) {
        AuxiliaryMap auxiliaryMap = new AuxiliaryMap();
        BeanUtils.copyProperties(mapParam,auxiliaryMap);
        auxiliaryMap.setId(UUIDGenerator.getUUID());
        auxiliaryMap.setDelFlag(YNEnum.N.toString());
        String userId = PlatformSessionUtils.getUserId();
        String userJson = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        auxiliaryMap.setCreateUser(userId);
        auxiliaryMap.setUploadUser(sUser.getUserName());
        auxiliaryMap.setUploadTime(PlatformDateUtils.getCurrentTimestamp());
        return auxiliaryMap;
    }

    @Override
    public Map<String, Object> getAll(QueryParam queryParam) {
        return auxiliaryMapRepository.getAll(queryParam);
    }

    @Override
    public AuxiliaryMap getById(String id) {
        return auxiliaryMapRepository.getById(id);
    }

    @Override
    public void deleteByIds(List<String> ids) {
        auxiliaryMapRepository.deleteByIds(ids);
    }

    @Override
    public void update(AuxiliaryMap auxiliaryMap) {
        MapParam mapParam = new MapParam();
        BeanUtils.copyProperties(auxiliaryMap,mapParam);
        List<AuxiliaryMap> check = auxiliaryMapRepository.check(mapParam);
        if (check.size() != 0){
            List<String> ids = check.stream().map(AuxiliaryMap::getId).collect(Collectors.toList());
            auxiliaryMapRepository.deleteByIds(ids);
        }
        auxiliaryMap.setUpdateUser(PlatformSessionUtils.getUserId());
        auxiliaryMap.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        auxiliaryMapRepository.update(auxiliaryMap);
    }

    @Override
    public Boolean check(MapParam mapParam) {
        List<AuxiliaryMap> auxiliaryMaps = auxiliaryMapRepository.check(mapParam);
        if (auxiliaryMaps.size() == 0){
            return false;
        }
        return true;
    }
}
